
package app;
import info.clearthought.layout.TableLayout;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Fuente extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5094823010396698806L;
	
	private JButton btnCerrar;
	private JLabel l4;
	private JLabel l5;
	private JLabel l3;
	private JLabel l2;
	private JLabel l1;
	private JTextField fuente1;
	private JTextField fuente5;
	private JTextField fuente4;
	private JTextField fuente3;
	private JTextField fuente2;
	private Main main;
	Image newimg = null;
	ImageIcon icon = null;
	BufferedImage imagen = null;
	private boolean [] libre = new boolean[5];
	private String[][] source= new String[5][4];
	@SuppressWarnings("unused")
	private static final String ACTION_CLOSE = "ACTION_CLOSE";
	 KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
	
	/**
	* Auto-generated main method to display this JFrame
	*/
		
	public Fuente(Main m) {
		super();
		this.main=m;
		addEscapeKey ();

		
		initGUI();
		reset();
	}
	private void addEscapeKey ()
	{
	// Handle escape key to close the dialog

	KeyStroke escape = KeyStroke.getKeyStroke (KeyEvent.VK_ESCAPE, 0, false);
	Action escapeAction = new AbstractAction ()
	{
	/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

	public void actionPerformed (ActionEvent e)
	{
		cancelActionPerformed(e);
	
	}
	};

	getRootPane ().getInputMap (JComponent.WHEN_IN_FOCUSED_WINDOW).put (escape, "ESCAPE");
	getRootPane ().getActionMap ().put ("ESCAPE", escapeAction);
	}
	
	private void cancelActionPerformed(ActionEvent evt) {
		
		main.obtenerFoco();
		this.dispose();
	}

	public ArrayList<String> getFuentes() {
		ArrayList<String> sources = new ArrayList<String>();
		if (!fuente1.getText().equals("")) sources.add(fuente1.getText());
		if (!fuente2.getText().equals("")) sources.add(fuente2.getText());
		if (!fuente3.getText().equals("")) sources.add(fuente3.getText());
		if (!fuente4.getText().equals("")) sources.add(fuente4.getText());
		if (!fuente5.getText().equals("")) sources.add(fuente5.getText());
		return sources;
	}
	
	
	
	public void reset() {
		fuente1.setText("");
		fuente2.setText("");
		fuente3.setText("");
		fuente4.setText("");
		fuente5.setText("");
		for (int i=0;i<=4;i++) libre[i]=true;
		l1.setIcon(null);
		l2.setIcon(null);
		l3.setIcon(null);
		l4.setIcon(null);
		l5.setIcon(null);
	
		fuente1.setEditable(true);
		fuente2.setEditable(true);
		fuente3.setEditable(true);
		fuente4.setEditable(true);
		fuente5.setEditable(true);
		fuente1.setEnabled(true);
		fuente2.setEnabled(true);
		fuente3.setEnabled(true);
		fuente4.setEnabled(true);
		fuente5.setEnabled(true);
		main.setCantFuentes(0);		
	}
	
	private void resetSinUsar() {
		if (fuente1.isEnabled()) fuente1.setText("");
		if (fuente2.isEnabled()) fuente2.setText("");
		if (fuente3.isEnabled()) fuente3.setText("");
		if (fuente4.isEnabled()) fuente4.setText("");
		if (fuente5.isEnabled()) fuente5.setText("");
	}
	
	public boolean checkFuentes() {
		return (!fuente1.getText().trim().equals("") || !fuente2.getText().trim().equals("") || !fuente3.getText().trim().equals("") || !fuente4.getText().trim().equals("") || !fuente5.getText().trim().equals("") );
		
	}
	
	public void setFocus() {
		fuente1.requestFocus();
	}
	
	
	public void setFuente(int i, String texto) {
		
		
		switch (i) {
		case 1: fuente1.setText(texto);break;
		case 2: fuente2.setText(texto);break;
		case 3: fuente3.setText(texto);break;
		case 4: fuente4.setText(texto);break;
		case 5: fuente5.setText(texto);break;
		
		}
	}
	
	public String getFuenteInfo(int i) {
		switch (i) {
		case 1: return fuente1.getText();
		case 2: return fuente2.getText();
		case 3: return fuente3.getText();
		case 4: return fuente4.getText();
		case 5: return fuente5.getText();
		default : return "";
		}
	}
	
	private void initGUI() {
		try {
			TableLayout thisLayout = new TableLayout(new double[][] {{7.0, 16.0, TableLayout.FILL, TableLayout.FILL, 67.0, TableLayout.FILL, 21.0, 7.0}, {7.0, TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, 7.0}});
			thisLayout.setHGap(5);
			thisLayout.setVGap(5);
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			this.addWindowListener(new WindowAdapter() {
		          public void windowClosing(WindowEvent e) {
		        	  cerrar();
		          }
		      });
			this.setTitle("Fuentes de Informacion");
			{
				fuente1 = new JTextField();
				fuente1.setName("fuente1");
				getContentPane().add(fuente1, "2, 1, 5, 1, f, c");
			}
			{
				fuente2 = new JTextField();
				fuente2.setName("fuente2");
				getContentPane().add(fuente2, "2, 2, 5, 2, f, c");
			}
			{
				fuente3 = new JTextField();
				fuente3.setName("fuente3");
				getContentPane().add(fuente3, "2, 3, 5, 3, f, c");
			}
			{
				fuente4 = new JTextField();
				fuente4.setName("fuente4");
				getContentPane().add(fuente4, "2, 4, 5, 4, f, c");
			}
			{
				fuente5 = new JTextField();
				fuente5.setName("fuente5");
				getContentPane().add(fuente5, "2, 5, 5, 5, f, c");
			}
			{
				btnCerrar = new JButton();
				getContentPane().add(btnCerrar, "3, 6, 4, 6, c, c");
				btnCerrar.setText("Cerrar");
				btnCerrar.setFont(new java.awt.Font("Ubuntu",0,12));
				btnCerrar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnCerrarActionPerformed(evt);
					}
				});
			}
			{

				getContentPane().add(getL1(), "1, 1");
				getContentPane().add(getL2(), "1, 2");
				getContentPane().add(getL3(), "1, 3");
				getContentPane().add(getL4(), "1, 4");
				getContentPane().add(getL5(), "1, 5");

			}
			
			pack();
			getPopup1();
			this.setSize(343, 304);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void btnCerrarActionPerformed(ActionEvent evt) {
		
		cerrar();
	}
	
	
	public void cerrar() {
		this.setVisible(false);
		ArrayList<String> array = new ArrayList<String>();
		if (!fuente1.getText().trim().equals(""))array.add(fuente1.getText());
		if (!fuente2.getText().trim().equals(""))array.add(fuente2.getText());
		if (!fuente3.getText().trim().equals(""))array.add(fuente3.getText());
		if (!fuente4.getText().trim().equals(""))array.add(fuente4.getText());
		if (!fuente5.getText().trim().equals(""))array.add(fuente5.getText());
		
		main.setCantFuentes(array.size());
		main.setFuentesInfo(array);	
			
		}
		
	
		
		//resetSinUsar();

	private JLabel getL1() {
		if(l1 == null) {
			l1 = new JLabel();
		}
		return l1;
	}
	
	private JLabel getL2() {
		if(l2 == null) {
			l2 = new JLabel();
		}
		return l2;
	}
	
	private JLabel getL3() {
		if(l3 == null) {
			l3 = new JLabel();
		}
		return l3;
	}
	
	private JLabel getL4() {
		if(l4 == null) {
			l4 = new JLabel();
		}
		return l4;
	}
	
	private JLabel getL5() {
		if(l5 == null) {
			l5 = new JLabel();
		}
		return l5;
	}

	//{"status":1,"data":{"id":"2f9704c729e7ed3b41647b7d0ad649fe","url":"http:\/\/www.yahoo.com\/","title":"yahoo.com","favicon":"http:\/\/www.google.com\/s2\/favicons?domain=www.yahoo.com"}}
	
	
	
	
	
	/********************************PopUp*******************************************/
	JPopupMenu popup1; 
	JMenuItem PopCortar1;
	JMenuItem PopPegar1;
	JMenuItem PopCopiar1;
	JMenuItem PopSelAll1;
	private String icon_copy="copy.gif";
	private String icon_paste="paste.gif";
	private String icon_cut="cut.gif";
	private String icon_select_all="selectall.gif";
	
	
	private JMenuItem getPopCortar1() {
		if(PopCortar1 == null) {
			PopCortar1 = new JMenuItem("Cortar");
			PopCortar1.setIcon(new ImageIcon(getClass().getResource("cut.gif")));
			PopCortar1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopCortar1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopCortar1ActionPerformed(evt);
				}
			});
		}
			return PopCortar1;
		}
	
	private JMenuItem getPopCopiar1() {
		if(PopCopiar1 == null) {
			PopCopiar1 = new JMenuItem("Copiar");
			PopCopiar1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopCopiar1.setIcon(new ImageIcon(getClass().getResource("copy.gif")));
			PopCopiar1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopCopiar1ActionPerformed(evt);
				}
			});
		}
			return PopCopiar1;
		}
			
			
	private JMenuItem getPopPegar1() {
		if(PopPegar1 == null) {
			PopPegar1 = new JMenuItem("Pegar");
			PopPegar1.setIcon(new ImageIcon(getClass().getResource("paste.gif")));
			PopPegar1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopPegar1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopPegar1ActionPerformed(evt);
				}
			});
		}
			return PopPegar1;
		}
	
	
	
	private JMenuItem getPopSelAll1() {
		if(PopSelAll1 == null) {
			PopSelAll1 = new JMenuItem("Seleccionar todo");
			PopSelAll1.setIcon(new ImageIcon(getClass().getResource(icon_select_all)));
			PopSelAll1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopSelAll1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopSelAll1ActionPerformed(evt);
				}
			});	
		}
			return PopSelAll1;
		}
	
	private void PopPegar1ActionPerformed(ActionEvent evt) {
		
		if (popup1.getInvoker().getName()=="fuente1")
    	{	
			fuente1.paste();
			fuente1.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente2")
    	{	
			fuente2.paste();
			fuente2.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente3")
    	{	
			fuente3.paste();
			fuente3.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente4")
    	{	
			fuente4.paste();
			fuente4.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente5")
    	{	
			fuente5.paste();
			fuente5.requestFocus();
    		    	}
    	
    	
    }
    
    private void PopCopiar1ActionPerformed(ActionEvent evt) {
    	
    	if (popup1.getInvoker().getName()=="fuente1")
    	{	
			fuente1.copy();
			
    		    	}
		if (popup1.getInvoker().getName()=="fuente2")
    	{	
			fuente2.copy();
			
    		    	}
		if (popup1.getInvoker().getName()=="fuente3")
    	{	
			fuente3.copy();
			
    		    	}
		if (popup1.getInvoker().getName()=="fuente4")
    	{	
			fuente4.copy();
			
    		    	}
		if (popup1.getInvoker().getName()=="fuente5")
    	{	
			fuente5.copy();
			
    		    	}
    	
    }
    
    
    
    private void PopSelAll1ActionPerformed(ActionEvent evt) {
    
    	if (popup1.getInvoker().getName()=="fuente1")
    	{	
			fuente1.selectAll();
			fuente1.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente2")
    	{	
			fuente2.selectAll();
			fuente2.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente3")
    	{	
			fuente3.selectAll();
			fuente3.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente4")
    	{	
			fuente4.selectAll();
			fuente4.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente5")
    	{	
			fuente5.selectAll();
			fuente5.requestFocus();
    		    	}
    }
    
	
    private void PopCortar1ActionPerformed(ActionEvent evt) {
    	if (popup1.getInvoker().getName()=="fuente1")
    	{	
			fuente1.cut();
			fuente1.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente2")
    	{	
			fuente2.cut();
			fuente2.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente3")
    	{	
			fuente3.cut();
			fuente3.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente4")
    	{	
			fuente4.cut();
			fuente4.requestFocus();
    		    	}
		if (popup1.getInvoker().getName()=="fuente5")
    	{	
			fuente5.cut();
			fuente5.requestFocus();
    		    	}
    }
    	
	
	
    private JPopupMenu getPopup1() {
		if(popup1 == null) {
			popup1 = new JPopupMenu();
			fuente1.setComponentPopupMenu (popup1);
			fuente2.setComponentPopupMenu (popup1);
			fuente3.setComponentPopupMenu (popup1);
			fuente4.setComponentPopupMenu (popup1);
			fuente5.setComponentPopupMenu (popup1);
			
			popup1.add(getPopCortar1());
			popup1.add(getPopCopiar1());
			popup1.add(getPopPegar1());
			popup1.add(getPopSelAll1());
			
		}
		return popup1;
		}
	
	
    /********************************PopUp*******************************************/

}
